#include <GameView.h>
#include <D3D9Renderer.h>

HumanView::HumanView()
{
	m_pProcessManager = ALE_NEW ProcessManager();
}

HumanView::~HumanView()
{
	SAFE_DELETE(m_pProcessManager);
}

HRESULT HumanView::VRestore()
{
	HRESULT hr = S_OK;
	
	for(auto it = m_screenElements.begin(); it != m_screenElements.end(); ++it)
	{
		HRESULT result = (*it)->VRestore();
		if (result != S_OK)
			hr = result;

	}
	return hr;
}

void HumanView::VUpdate(double deltaSecs)
{
	for(auto it = m_screenElements.begin(); it != m_screenElements.end(); ++it)
	{
		(*it)->VUpdate(deltaSecs);
	}
	m_pProcessManager->UpdateProcesses(deltaSecs);
}

void HumanView::VRender()
{
	HRESULT hr;
	HR(g_pRenderer->GetDevice()->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

	if (SUCCEEDED(g_pRenderer->GetDevice()->BeginScene() ))
	{
		m_screenElements.sort(SortBy_SharedPtr_Content<IScreenElement>());
		for(auto it = m_screenElements.begin(); it != m_screenElements.end(); ++it)
		{
			if ( (*it)->VIsVisible() )
			{
				hr = (*it)->VRender();
			}
		}
	}

	HR(g_pRenderer->GetDevice()->EndScene() );
	HR(g_pRenderer->GetDevice()->Present(0,0,0,0));
}

void HumanView::VPushElement(ScreenElementPtr pElement)
{
	m_screenElements.push_front(pElement);
}

void HumanView::VPopElement(ScreenElementPtr pElement)
{
	m_screenElements.remove(pElement);
}